package OT.shoufouni.serverThreads;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import OT.shoufouni.gpsMaps.findRoute;
import OT.shoufouni.mainGUI.addFriendFrame;
import OT.shoufouni.mainGUI.myLocation;
import android.util.Log;

public class serverLocateFriendQuaryThread extends Thread {
   private final addFriendFrame callerActivity1;
   private final findRoute callerActivity2;
   private final String idToLocate, myName;
   int mod;
   public serverLocateFriendQuaryThread(addFriendFrame callerActivity, String name, String myNAme) {
      this.callerActivity1 = callerActivity;
      callerActivity2 = null;
      this.idToLocate = name;
      this.myName = myNAme;
      mod = 1;
    }
   public serverLocateFriendQuaryThread(findRoute callerActivity, String name, String myNAme) {
	   callerActivity1 = null;   
	   this.callerActivity2 = callerActivity;
	      this.idToLocate = name;
	      this.myName = myNAme;
	      mod = 2;
	    }
   public void run() {
	   ArrayList<myLocation>receivedData = httpTranslateGet(idToLocate);
	   if (0==receivedData.size()){
		   receivedData = null;
		   return;
	   }
	   // addFriendFrame query
	   if (1==mod){
		   callerActivity1.responseDisplay(receivedData.get(0));
	   }
	   // findRoute query
	   else{
		   callerActivity2.responseDisplayFriendLocation(receivedData.get(0));
	   }   
   }
   private ArrayList<myLocation> httpTranslateGet(String idToLocate) {
	   String requestUrl;
	   StringBuilder builder = new StringBuilder();
	   ArrayList<myLocation> locations = new ArrayList<myLocation>();
       try {
           Log.d("serverLocateFriendQuaryThread ",idToLocate);

    	   requestUrl = "http://10.0.2.2:8080/serverRestSide2/findFriend/"+idToLocate;
    	   try {
               URL url = new URL(requestUrl.toString());
               BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
               String inputLine = null;
               Log.d("serverLocateFriendQuaryThread ","-----RESPONSE START-----" +" id="+ Thread.currentThread().getId());
               
               while ((inputLine = in.readLine()) != null) {
            	   builder.append(inputLine);
            	   if ("BAD".equals(inputLine)){
            		   return null;
            	   }
            	   String [] tempString=inputLine.split("\\,");
            	   locations.add(new myLocation(Double.parseDouble(tempString[0]),Double.parseDouble(tempString[1])));
                   Log.d("serverLocateFriendQuaryThread ",locations.get(0).getX()+","+locations.get(0).getY());

               }
               in.close();
               Log.d("serverLocateFriendQuaryThread ",builder.toString());
               Log.d("serverLocateFriendQuaryThread ","-----RESPONSE END-----" +" id="+ Thread.currentThread().getId());
               
               
           } catch (IOException e) {
        	   Log.e("serverLocateFriendQuaryThread ",e.getMessage());
               return null;
           }
       }finally{
       }
       
       return locations;

   	}
   
}
